package com.zndroid.notification.box;

import android.app.Notification;
import android.app.PendingIntent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.provider.Settings;
import android.widget.RemoteViews;

import androidx.annotation.DrawableRes;
import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.core.app.NotificationCompat;

/**
 * 通知展示形式实体
 * @author lzy
 * @date 2021/2/8
 */
public class Trumpet {
    private boolean onGoing;
    @NonNull
    private int id = -1;
    @DrawableRes
    private int smallIcon = -1;
    @DrawableRes
    private int bigIconRes = -1;
    @LayoutRes
    private int remoteLayoutId;
    private int defaults;
    private int priority;
    private String title = "";
    private String message = "";
    private String channelId = "";
    private String channelName = "";
    private Bitmap bigIcon;
    private Bitmap bigPicture;
    private PendingIntent actionPending;
    private PendingIntent deletePending;
    private RemoteViews remoteViews;
    private Uri soundUri = Settings.System.DEFAULT_NOTIFICATION_URI;
    private boolean soundEnable = true;
    private boolean lightEnable = false;
    private boolean vibrateEnable = false;
    private boolean silentEnable = false;

    public void setBigIcon(Bitmap bigIcon) {
        this.bigIcon = bigIcon;
    }

    public Bitmap getBigIcon() {
        return bigIcon;
    }

    public int getBigIconRes() {
        return bigIconRes;
    }

    public int getSmallIcon() {
        return smallIcon;
    }

    public String getTitle() {
        return title;
    }

    public String getMessage() {
        return message;
    }

    public PendingIntent getActionPending() {
        return actionPending;
    }

    public String getChannelId() {
        return channelId;
    }

    public int getId() {
        return id;
    }

    public Bitmap getBigPicture() {
        return bigPicture;
    }

    public PendingIntent getDeletePending() {
        return deletePending;
    }

    public int getDefaults() {
        return defaults;
    }

    public boolean isOnGoing() {
        return onGoing;
    }

    public int getPriority() {
        return priority;
    }

    public RemoteViews getRemoteViews() {
        return remoteViews;
    }

    public void setRemoteViews(RemoteViews remoteViews) {
        this.remoteViews = remoteViews;
    }

    public int getRemoteLayoutId() {
        return remoteLayoutId;
    }

    public String getChannelName() {
        return channelName;
    }

    public boolean isSoundEnable() {
        return soundEnable;
    }

    public boolean isLightEnable() {
        return lightEnable;
    }

    public boolean isVibrateEnable() {
        return vibrateEnable;
    }

    public boolean isSilentEnable() {
        return silentEnable;
    }

    public Uri getSoundUri() {
        return soundUri;
    }

    public static class Builder {
        private boolean onGoing = false;
        @NonNull
        private int id = -1;
        @DrawableRes
        private int smallIcon = -1;
        @DrawableRes
        private int bigIconRes = -1;
        private int defaults = NotificationCompat.DEFAULT_ALL;
        /**
         * Default notification priority for {@link NotificationCompat.Builder#setPriority(int)}.
         * If your application does not prioritize its own notifications,
         * use this value for all notifications.
         */
        private int priority = NotificationCompat.PRIORITY_DEFAULT;
        private String title = "";
        private String message = "";
        private String channelId = "com.zndroid.default.channelId";
        private String channelName = "com.zndroid.default.channelName";
        private Bitmap bigIcon;
        private Bitmap bigPicture;
        private PendingIntent actionPending;
        private PendingIntent deletePending;
        private RemoteViews remoteViews;

        @LayoutRes
        private int remoteLayoutId = -1;
        private Uri soundUri;
        private boolean soundEnable = true;
        private boolean lightEnable = false;
        private boolean vibrateEnable = false;
        private boolean silentEnable = false;

        public Builder setTitle(String title) {
            this.title = title;
            return this;
        }

        public Builder setMessage(String message) {
            this.message = message;
            return this;
        }

        /**
         * Android从5.0系统开始，现在Google要求，所有应用程序的通知栏图标，应该只使用alpha图层来进行绘制，而不应该包括RGB图层。
         * 即：纯色
         * */
        public Builder setSmallIcon(int smallIcon) {
            this.smallIcon = smallIcon;
            return this;
        }

        public Builder setBigIcon(Bitmap bigIcon) {
            this.bigIcon = bigIcon;
            return this;
        }

        public Builder setBigIcon(int bigIconRes) {
            this.bigIconRes = bigIconRes;
            return this;
        }

        public Builder setActionPending(PendingIntent actionPending) {
            this.actionPending = actionPending;
            return this;
        }

        public Builder setDeletePending(PendingIntent deletePending) {
            this.deletePending = deletePending;
            return this;
        }

        public Builder setChannelId(String channelId) {
            this.channelId = channelId;
            return this;
        }

        public Builder setChannelName(String channelName) {
            this.channelName = channelName;
            return this;
        }

        public Builder setId(int id) {
            this.id = id;
            return this;
        }

        public Builder setBigPicture(Bitmap bigPicture) {
            this.bigPicture = bigPicture;
            return this;
        }

        public Builder setOnGoing(boolean onGoing) {
            this.onGoing = onGoing;
            return this;
        }

        /**
         * Set the default notification options that will be used.
         * <p>
         * The value should be one or more of the following fields combined with
         * bitwise-or:
         * {@link Notification#DEFAULT_SOUND}, {@link Notification#DEFAULT_VIBRATE},
         * {@link Notification#DEFAULT_LIGHTS}.
         * <p>
         * For all default values, use {@link Notification#DEFAULT_ALL}.
         */
        public Builder setDefaults(int defaults) {
            this.defaults = defaults;
            return this;
        }

        public Builder withSilent() {
            this.silentEnable = true;
            return this;
        }

        /**
         * {@link #setDefaults(int)}
         * */
        public Builder withSound() {
            this.defaults = this.defaults | NotificationCompat.DEFAULT_SOUND;
            this.soundEnable = true;
            return this;
        }

        public Builder withSound(Uri soundUri) {
            this.defaults = this.defaults | NotificationCompat.DEFAULT_SOUND;
            this.soundEnable = true;
            this.soundUri = soundUri;
            return this;
        }

        /**
         * {@link #setDefaults(int)}
         * */
        public Builder withLight() {
            this.defaults = this.defaults | NotificationCompat.DEFAULT_LIGHTS;
            return this;
        }

        /**
         * {@link #setDefaults(int)}
         * */
        public Builder withVibrate() {
            this.defaults = this.defaults | NotificationCompat.DEFAULT_VIBRATE;
            return this;
        }

        public Builder setPriority(int priority) {
            this.priority = priority;
            return this;
        }

        /**
         * Higher notification priority for {@link #setPriority(int)},
         * for more important notifications or alerts. The UI may choose
         * to show these items larger, or at a different position in
         * notification lists, compared with your app's {@link NotificationCompat#PRIORITY_DEFAULT} items.
         */
        public Builder importanceHigh() {
            this.priority = NotificationCompat.PRIORITY_HIGH;
            return this;
        }

        /**
         * Lower notification priority for {@link NotificationCompat.Builder#setPriority(int)},
         * for items that are less important. The UI may choose to show
         * these items smaller, or at a different position in the list,
         * compared with your app's {@link NotificationCompat#PRIORITY_DEFAULT} items.
         */
        public Builder importanceLow() {
            this.priority = NotificationCompat.PRIORITY_LOW;
            return this;
        }

        /**
         * Lowest notification priority for {@link #setPriority(int)};
         * these items might not be shown to the user except under
         * special circumstances, such as detailed notification logs.
         */
        public Builder importanceMin() {
            this.priority = NotificationCompat.PRIORITY_MIN;
            return this;
        }

        /**
         * Highest notification priority for {@link NotificationCompat.Builder#setPriority(int)},
         * for your application's most important items that require the user's
         * prompt attention or input.
         */
        public Builder importanceMax() {
            this.priority = NotificationCompat.PRIORITY_MAX;
            return this;
        }

        public Builder setRemoteViews(RemoteViews remoteViews) {
            this.remoteViews = remoteViews;
            return this;
        }

        public Builder setRemoteViews(int remoteLayoutId) {
            this.remoteLayoutId = remoteLayoutId;
            return this;
        }

        public Trumpet build() {
            Trumpet trumpet = new Trumpet();
            apply(trumpet);
            return trumpet;
        }

        private void apply(Trumpet trumpet) {
            trumpet.id = this.id;
            trumpet.bigIcon = this.bigIcon;
            trumpet.bigIconRes = this.bigIconRes;
            trumpet.smallIcon = this.smallIcon;
            trumpet.message = this.message;
            trumpet.channelId = this.channelId;
            trumpet.channelName = channelName;
            trumpet.title = this.title;
            trumpet.actionPending = this.actionPending;
            trumpet.deletePending = this.deletePending;
            trumpet.bigPicture = this.bigPicture;
            trumpet.defaults = this.defaults;
            trumpet.onGoing = this.onGoing;
            trumpet.priority = this.priority;
            trumpet.remoteViews = this.remoteViews;
            trumpet.remoteLayoutId = this.remoteLayoutId;
            trumpet.soundEnable = this.soundEnable;
            trumpet.lightEnable = this.lightEnable;
            trumpet.vibrateEnable = this.vibrateEnable;
            trumpet.silentEnable = this.silentEnable;
            trumpet.soundUri = this.soundUri;
        }
    }
}
